package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.detail;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommentResult;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class DynamicCommentChildAdapter extends BaseQuickAdapter<CommentResult, BaseViewHolder> {

    private int parentPos;

    public void setParentPos(int parentPos) {
        this.parentPos = parentPos;
    }

    public DynamicCommentChildAdapter() {
        this(new ArrayList<>());
    }

    public DynamicCommentChildAdapter(List<CommentResult> data, int pos) {
        this(data);
        parentPos = pos;
    }

    public DynamicCommentChildAdapter(List<CommentResult> data) {
        super(R.layout.item_comment_child, data);
    }

    public long getLastItemId() {
        return getData().size() == 0 ? 0 : getItemId(getData().size() - 1);
    }

    public int getParentPos() {
        return parentPos;
    }

    @Override
    public long getItemId(int position) {
        CommentResult item = getItem(position);
        if (item == null) {
            return -1;
        }
        return item.getId();
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, CommentResult item) {
        String fromUser = item.getFromNickname();
        String toUser = item.getToNickname();
        String content = item.getCommentContent();
        SpannableString spannableString;
        ForegroundColorSpan colorSpan01 =
                new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorAccent));
        if (!StringUtils.isEmpty(toUser)) {
            ForegroundColorSpan colorSpan02 =
                    new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorAccent));
            spannableString = new SpannableString(String.format("%s 回复 %s : %s", fromUser, toUser, content));
            spannableString.setSpan(colorSpan01, 0, fromUser.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(colorSpan02, fromUser.length() + 4, fromUser.length() + 4 + toUser.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            spannableString = new SpannableString(String.format("%s : %s", fromUser, content));
            spannableString.setSpan(colorSpan01, 0, fromUser.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        helper.setText(R.id.tv_text, spannableString);
    }
}
